package com.shanxia.codec.cmd;

import com.shanxia.codec.ProtocolDataBase;
import com.shanxia.codec.enums.DecodeEnum;
import lombok.Data;

import java.math.BigDecimal;

import static com.shanxia.codec.CodeConstant.HUNDRED;
import static com.shanxia.codec.CodeConstant.PERCENT;
import static com.shanxia.codec.Decoder.*;

/**
 * S -> C
 * 后台服务器设置 24 时电费计价策略信息
 */
@Data
public class Cmd1105 extends ProtocolDataBase {

    public static final int MIN_SIZE = 144;

    private int startHour1;                         // 开始小时
    private int startMinute1;                       // 开始分钟
    private int endHour1;                           // 结束小时
    private int endMinute1;                         // 结束分钟
    private double chargeRate1;                     // 充电费率
    private double serviceRate1;                    // 服务费率
    private int startHour2;
    private int startMinute2;
    private int endHour2;
    private int endMinute2;
    private double chargeRate2;
    private double serviceRate2;
    private int startHour3;
    private int startMinute3;
    private int endHour3;
    private int endMinute3;
    private double chargeRate3;
    private double serviceRate3;
    private int startHour4;
    private int startMinute4;
    private int endHour4;
    private int endMinute4;
    private double chargeRate4;
    private double serviceRate4;
    private int startHour5;
    private int startMinute5;
    private int endHour5;
    private int endMinute5;
    private double chargeRate5;
    private double serviceRate5;
    private int startHour6;
    private int startMinute6;
    private int endHour6;
    private int endMinute6;
    private double chargeRate6;
    private double serviceRate6;
    private int startHour7;
    private int startMinute7;
    private int endHour7;
    private int endMinute7;
    private double chargeRate7;
    private double serviceRate7;
    private int startHour8;
    private int startMinute8;
    private int endHour8;
    private int endMinute8;
    private double chargeRate8;
    private double serviceRate8;
    private int startHour9;
    private int startMinute9;
    private int endHour9;
    private int endMinute9;
    private double chargeRate9;
    private double serviceRate9;
    private int startHour10;
    private int startMinute10;
    private int endHour10;
    private int endMinute10;
    private double chargeRate10;
    private double serviceRate10;
    private int startHour11;
    private int startMinute11;
    private int endHour11;
    private int endMinute11;
    private double chargeRate11;
    private double serviceRate11;
    private int startHour12;
    private int startMinute12;
    private int endHour12;
    private int endMinute12;
    private double chargeRate12;
    private double serviceRate12;

    public Cmd1105(){
        super.setCmd(1105);
    }

    public static Cmd1105 decodeFromBase(ProtocolDataBase protocolDataBase) {
        Cmd1105 cmd = new Cmd1105();
        cmd.copyDataFrom(protocolDataBase);
        //数据域长度
        byte[] dataField = protocolDataBase.getDataField();
        if (dataField.length < MIN_SIZE) {
            cmd.setResult(DecodeEnum.InvalidData);
            return cmd;
        }
        int offset = 0;
        // 1字节开始小时
        cmd.startHour1 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute1 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour1 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute1 = getUint8(dataField[offset++]);
        // 4字节充电费率1
        cmd.chargeRate1 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率1
        cmd.serviceRate1 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour2 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute2 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour2 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute2 = getUint8(dataField[offset++]);
        // 4字节充电费率2
        cmd.chargeRate2 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率2
        cmd.serviceRate2 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour3 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute3 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour3 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute3 = getUint8(dataField[offset++]);
        // 4字节充电费率3
        cmd.chargeRate3 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率3
        cmd.serviceRate3 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour4 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute4 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour4 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute4 = getUint8(dataField[offset++]);
        // 4字节充电费率4
        cmd.chargeRate4 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率4
        cmd.serviceRate4 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour5 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute5 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour5 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute5 = getUint8(dataField[offset++]);
        // 4字节充电费率5
        cmd.chargeRate5 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率5
        cmd.serviceRate5 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour6 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute6 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour6 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute6 = getUint8(dataField[offset++]);
        // 4字节充电费率6
        cmd.chargeRate6 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率6
        cmd.serviceRate6 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour7 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute7 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour7 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute7 = getUint8(dataField[offset++]);
        // 4字节充电费率7
        cmd.chargeRate7 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率7
        cmd.serviceRate7 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour8 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute8 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour8 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute8 = getUint8(dataField[offset++]);
        // 4字节充电费率8
        cmd.chargeRate8 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率8
        cmd.serviceRate8 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour9 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute9 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour9 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute9 = getUint8(dataField[offset++]);
        // 4字节充电费率9
        cmd.chargeRate9 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率9
        cmd.serviceRate9 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour10 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute10 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour10 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute10 = getUint8(dataField[offset++]);
        // 4字节充电费率10
        cmd.chargeRate10 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率10
        cmd.serviceRate10 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour11 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute11 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour11 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute11 = getUint8(dataField[offset++]);
        // 4字节充电费率12
        cmd.chargeRate12 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率12
        cmd.serviceRate12 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour12 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute12 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour12 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute12 = getUint8(dataField[offset++]);
        // 4字节充电费率12
        cmd.chargeRate12 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率12
        cmd.serviceRate12 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        return cmd;
    }

    @Override
    public byte[] getDataField() {
        byte[] data = new byte[MIN_SIZE];
        int offset = 0;
        // 1字节开始小时
        data[offset++] = (byte) startHour1;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute1;
        // 1字节结束小时
        data[offset++] = (byte) endHour1;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute1;
        // 4字节充电费率1
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(chargeRate1)).multiply(HUNDRED).intValue());
        // 4字节服务费率1
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(serviceRate1)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour2;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute2;
        // 1字节结束小时
        data[offset++] = (byte) endHour2;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute2;
        // 4字节充电费率2
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(chargeRate2)).multiply(HUNDRED).intValue());
        // 4字节服务费率2
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(serviceRate2)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour3;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute3;
        // 1字节结束小时
        data[offset++] = (byte) endHour3;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute3;
        // 4字节充电费率3
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(chargeRate3)).multiply(HUNDRED).intValue());
        // 4字节服务费率3
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(serviceRate3)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour4;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute4;
        // 1字节结束小时
        data[offset++] = (byte) endHour4;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute4;
        // 4字节充电费率4
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(chargeRate4)).multiply(HUNDRED).intValue());
        // 4字节服务费率4
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(serviceRate4)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour5;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute5;
        // 1字节结束小时
        data[offset++] = (byte) endHour5;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute5;
        // 4字节充电费率5
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(chargeRate5)).multiply(HUNDRED).intValue());
        // 4字节服务费率5
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(serviceRate5)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour6;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute6;
        // 1字节结束小时
        data[offset++] = (byte) endHour6;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute6;
        // 4字节充电费率6
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(chargeRate6)).multiply(HUNDRED).intValue());
        // 4字节服务费率6
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(serviceRate6)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour7;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute7;
        // 1字节结束小时
        data[offset++] = (byte) endHour7;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute7;
        // 4字节充电费率7
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(chargeRate7)).multiply(HUNDRED).intValue());
        // 4字节服务费率7
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(serviceRate7)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour8;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute8;
        // 1字节结束小时
        data[offset++] = (byte) endHour8;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute8;
        // 4字节充电费率8
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(chargeRate8)).multiply(HUNDRED).intValue());
        // 4字节服务费率8
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(serviceRate8)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour9;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute9;
        // 1字节结束小时
        data[offset++] = (byte) endHour9;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute9;
        // 4字节充电费率9
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(chargeRate9)).multiply(HUNDRED).intValue());
        // 4字节服务费率9
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(serviceRate9)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour10;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute10;
        // 1字节结束小时
        data[offset++] = (byte) endHour10;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute10;
        // 4字节充电费率10
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(chargeRate10)).multiply(HUNDRED).intValue());
        // 4字节服务费率10
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(serviceRate10)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour11;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute11;
        // 1字节结束小时
        data[offset++] = (byte) endHour11;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute11;
        // 4字节充电费率11
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(chargeRate11)).multiply(HUNDRED).intValue());
        // 4字节服务费率11
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(serviceRate11)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour12;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute12;
        // 1字节结束小时
        data[offset++] = (byte) endHour12;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute12;
        // 4字节充电费率12
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(chargeRate12)).multiply(HUNDRED).intValue());
        // 4字节服务费率12
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(serviceRate12)).multiply(HUNDRED).intValue());
        return data;
    }
}
